package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.exception.PlatformBizErrorCodes;
import com.iwomy.secureplat.common.core.util.MsgUtils;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.excel.annotation.RequestExcel;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.platform.entity.BasicSensorEntity;
import com.iwomy.secureplat.platform.service.BasicSensorService;
import com.iwomy.secureplat.platform.vo.BasicSensorExcelVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 传感器
 *
 * @author pig
 * @date 2023-12-22 19:16:00
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicSensor")
@Tag(description = "basicSensor", name = "传感器管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicSensorController {

	private final BasicSensorService basicSensorService;

	/**
	 * 分页查询
	 *
	 * @param page        分页对象
	 * @param basicSensor 传感器
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_basicSensor_view')")
	public R getBasicSensorPage(@ParameterObject Page page, @ParameterObject BasicSensorEntity basicSensor) {
		return R.ok(basicSensorService.page(page, Wrappers.lambdaQuery(basicSensor)));
	}

	/**
	 * 分页查询
	 *
	 * @param basicSensor 传感器
	 * @return
	 */
	@Operation(summary = "列表查询", description = "列表查询")
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('platform_basicSensor_view')")
	public R list(@ParameterObject BasicSensorEntity basicSensor) {
		return R.ok(basicSensorService.list(Wrappers.lambdaQuery(basicSensor).eq(BasicSensorEntity::getState,20)));
	}


	/**
	 * 通过id查询传感器
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicSensor_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(basicSensorService.getById(id));
	}

	/**
	 * 新增传感器
	 *
	 * @param basicSensor 传感器
	 * @return R
	 */
	@Operation(summary = "新增传感器", description = "新增传感器")
	@SysLog("新增传感器")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_basicSensor_add')")
	public R save(@RequestBody BasicSensorEntity basicSensor) {

		basicSensor.setSensorTypeCode(basicSensor.getSensorTypeCode());
		LambdaQueryWrapper<BasicSensorEntity> wrapper = Wrappers.lambdaQuery(BasicSensorEntity.class)
				.eq(BasicSensorEntity::getSensorTypeCode, basicSensor.getSensorTypeCode());
		BasicSensorEntity one = basicSensorService.getOne(wrapper);
//		if (ObjectUtil.isNotNull(one)) return R.failed("传感器ID已经存在，不允许新增");
		if (ObjectUtil.isNotNull(one)) return R.failed(MsgUtils.getMessage(PlatformBizErrorCodes.SENSOR_ID_EXIST));


//		basicSensor.setDeptId(SecurityUtils.getUser().getDeptId());
		return R.ok(basicSensorService.save(basicSensor));
	}

	/**
	 * 修改传感器
	 *
	 * @param basicSensor 传感器
	 * @return R
	 */
	@Operation(summary = "修改传感器", description = "修改传感器")
	@SysLog("修改传感器")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicSensor_edit')")
	public R updateById(@RequestBody BasicSensorEntity basicSensor) {
		basicSensor.setSensorTypeCode(basicSensor.getSensorTypeCode());
		LambdaQueryWrapper<BasicSensorEntity> wrapper = Wrappers.lambdaQuery(BasicSensorEntity.class)
				.eq(BasicSensorEntity::getSensorTypeCode, basicSensor.getSensorTypeCode());
		BasicSensorEntity one = basicSensorService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one) &&!ObjectUtil.equal(one.getId(),basicSensor.getId())) return R.failed(MsgUtils.getMessage(PlatformBizErrorCodes.SENSOR_ID_USED_MODIFY));
		return R.ok(basicSensorService.updateById(basicSensor));
	}

	/**
	 * 通过id删除传感器
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除传感器", description = "通过id删除传感器")
	@SysLog("通过id删除传感器")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicSensor_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(basicSensorService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicSensor 查询条件
	 * @param ids         导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicSensor_export')")
	public List<BasicSensorEntity> export(BasicSensorEntity basicSensor, Long[] ids) {
		return basicSensorService.list(Wrappers.lambdaQuery(basicSensor).in(ArrayUtil.isNotEmpty(ids), BasicSensorEntity::getId, ids));
	}

	/**
	 * 启用禁用
	 *
	 * @param basicSensorEntity
	 * @return R
	 */
	@Operation(summary = "启用禁用传感器类型", description = "启用禁用传感器类型")
	@SysLog(expression = "'启用禁用传感器类型'.concat(#basicSensorEntity.remarks)")
	@PostMapping("/enableOrDisable")
	public R enableOrDisable(@RequestBody BasicSensorEntity basicSensorEntity) {
		return R.ok(basicSensorService.updateById(basicSensorEntity));
	}

	/**
	 * 导入excel 表格
	 */
	@PostMapping("/import")
	public R importSensor(@RequestExcel List<BasicSensorExcelVO> excelVOList, BindingResult bindingResult) {
		System.out.println("excelVOList========="+excelVOList);
		return basicSensorService.importSensor(excelVOList, bindingResult);
	}

}